package com.googlecode.todo_robot.parser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Fuad Ibrahimov
 * @since 22.12.2009
 */
public class RegexTodoParser implements TodoParser {
  public RegexTodoParser(String text) { // TODO there is no need doing parsing in the constructor. A service object would be a better choice.
    if(text != null && !text.isEmpty()) {
      parse(text);
    }
  }

  private void parse(String text) {
    Matcher matcher = PATTERN.matcher(text);
    while(matcher.find()) {
      if(matcher.groupCount() > 0) {
        this.todos.add(matcher.group(1));
      }
    }
  }

  @Override
  public List<String> todos() {
    return Collections.unmodifiableList(todos);
  }

  private final List<String> todos = new ArrayList<String>();
  private static final Pattern PATTERN = Pattern.compile("(TODO:\\s*([^.]+))\\.\\s*"); // NON-NLS
}
